<template>
  <div class="elimination-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h1 class="page-title">淘汰管理</h1>
      <p class="page-description">管理羊只淘汰信息,支持条件查询与分页</p>
    </div>

    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline class="search-form">

        <el-form-item label="淘汰日期">
          <el-date-picker v-model="searchForm.EliminationDate" type="date" placeholder="请选择淘汰日期" class="search-input" />
        </el-form-item>
        <el-form-item label="羊只类型">
          <el-select v-model="searchForm.SheepTypeId" placeholder="请选择羊只类型" clearable class="search-input">
            <el-option v-for="item in sheepTypeList" :key="item.Id" :label="item.Name" :value="item.Id" />
          </el-select>
        </el-form-item>
        <el-form-item label="栋舍">
          <el-select v-model="searchForm.PenId" placeholder="请选择栋舍" clearable class="search-input">
            <el-option v-for="item in penList" :key="item.House_typeId" :label="item.DongsheName" :value="item.House_typeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="栏位">
          <el-input v-model="searchForm.Field" placeholder="请输入栏位" clearable class="search-input" />
        </el-form-item>
        <el-form-item label="耳号">
          <el-select v-model="searchForm.EarNumber" placeholder="请选择耳号" clearable class="search-input">
            <el-option v-for="item in earNumberOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否离场">
          <el-select v-model="searchForm.LeaveStatus" placeholder="请选择离场状态" clearable class="search-input">
            <el-option label="已离场" :value="true" />
            <el-option label="未离场" :value="false" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" class="search-btn">查询</el-button>
          <el-button type="success" @click="goAdd" class="add-btn">新增淘汰</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <el-table :data="tableData" stripe class="data-table">
        <el-table-column type="index" label="编号" width="80" align="center" />
        <el-table-column prop="EliminationDate" label="淘汰日期" width="120" align="center">
          <template #default="scope">
            {{ formatDate(scope.row.EliminationDate) }}
          </template>
        </el-table-column>
       
        <el-table-column prop="SheepTypeName" label="羊只类型" width="150" align="center" />
        <el-table-column prop="SheepFilesId" label="耳号" width="120" align="center" />
        <el-table-column prop="HouseName" label="栋舍" width="150" align="center" />
        <el-table-column prop="FieldName" label="栏位" width="100" align="center" />
        <el-table-column prop="Breed" label="品种" width="80" align="center" />
        <el-table-column prop="LeaveStatus" label="是否离场" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.LeaveStatus ? 'success' : 'warning'">
              {{ scope.row.LeaveStatus ? '已离场' : '未离场' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="EliminationReason" label="淘汰原因" width="200" align="center" />
        <el-table-column prop="Handling_measuresName" label="处理措施" width="200" align="center" />
        <el-table-column label="操作" width="150" align="center">
          <template #default="scope">
            <el-button type="danger" size="small" @click="deleteElimination(scope.row)" class="delete-btn">删除</el-button>
            <el-button type="primary" size="small" @click="editElimination(scope.row)" class="edit-btn">修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <div class="pagination-info">
          共{{ total }}条
        </div>
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import service from '@/utils/request'

const router = useRouter()

// 响应式数据
const tableData = ref<any[]>([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 搜索表单
const searchForm = reactive({
  EliminatorId: '',
  EliminationDate: '',
  SheepTypeId: '',
  PenId: '',
  Field: '',
  EarNumber: '',
  LeaveStatus: null as boolean | null
})

// 下拉选项数据
const eliminatorList = ref<any[]>([])
const sheepTypeList = ref<any[]>([])
const penList = ref<any[]>([])
const earNumberOptions = ref<string[]>([])



// 获取羊只类型列表
const fetchSheepTypeList = () => {
  service.get('/api/SheepType/List/list', { baseURL: 'http://39.96.200.62:8511' }).then((res: any) => {
    if (res.data && res.data.code === 200) {
      sheepTypeList.value = res.data.data || []
    }
  })
}

// 获取栋舍列表
const fetchPenList = () => {
  service.get('/api/Pen/List', { baseURL: 'http://39.96.200.62:8511' }).then((res: any) => {
    if (res.data && res.data.code === 200) {
      penList.value = res.data.data || []
    }
  })
}

// 获取耳号列表
const fetchEarNumbers = () => {
  service.get('/api/EliminationInfo/GetSheepFilesIDEarNumber', { baseURL: 'http://39.96.200.62:8411' }).then((res: any) => {
    earNumberOptions.value = (res.data.data || []).map((item: any) => item.Name)
  })
}

// 获取表格数据
const fetchList = () => {
  const params = {
    page: currentPage.value,
    pageSize: pageSize.value,
    ...searchForm,
    _t: Date.now()
  }
  
  service.get('/api/CullingManagement/Search/search', { params, baseURL: 'http://39.96.200.62:8511' }).then((res: any) => {
    if (res.data && res.data.code === 200) {
      tableData.value = res.data.data || []
      total.value = res.data.total || 0
    }
  }).catch((error: any) => {
    ElMessage.error('获取数据失败')
    console.error('获取淘汰管理数据失败:', error)
  })
}

// 查询
const handleSearch = () => {
  currentPage.value = 1
  fetchList()
}

// 分页处理
const handleSizeChange = (val: number) => {
  pageSize.value = val
  fetchList()
}

const handleCurrentChange = (val: number) => {
  currentPage.value = val
  fetchList()
}

// 删除淘汰记录
const deleteElimination = (row: any) => {
  ElMessageBox.confirm('确定要删除这条淘汰记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    service.delete(`/api/CullingManagement/Delete/delete?Id=${row.Id}`, { baseURL: 'http://39.96.200.62:8511' }).then((res: any) => {
      if (res.data && res.data.code === 200) {
        ElMessage.success('删除成功')
        fetchList()
      } else {
        ElMessage.error(res.data.msg || '删除失败')
      }
    }).catch((error: any) => {
      ElMessage.error('删除失败')
      console.error('删除淘汰记录失败:', error)
    })
  })
}

// 编辑淘汰记录
const editElimination = (row: any) => {
  router.push({
    path: '/EliminationAdd',
    query: { id: row.Id }
  })
}

// 新增淘汰记录
const goAdd = () => {
  router.push('/EliminationAdd')
}

// 格式化日期
const formatDate = (date: string) => {
  if (!date) return ''
  return new Date(date).toLocaleDateString()
}

// 页面加载时获取数据
onMounted(() => {
  fetchSheepTypeList()
  fetchPenList()
  fetchEarNumbers()
  fetchList()
})
</script>

<style scoped>
.elimination-container {
  min-height: 100vh;
  background: #f7f8fa;
  padding: 24px;
}

/* 页面标题样式 */
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 32px;
  border-radius: 12px;
  margin-bottom: 24px;
  text-align: center;
  color: white;
}

.page-title {
  font-size: 32px;
  font-weight: bold;
  margin: 0 0 8px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-description {
  font-size: 16px;
  margin: 0;
  opacity: 0.9;
}

/* 搜索卡片样式 */
.search-card {
  margin-bottom: 24px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end;
}

.search-input {
  width: 200px;
}

.search-btn {
  background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 600;
}

.add-btn {
  background: linear-gradient(135deg, #67C23A 0%, #85ce61 100%);
  border: none;
  padding: 12px 24px;
  border-radius: 8px;
  font-weight: 600;
}

/* 表格卡片样式 */
.table-card {
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.data-table {
  width: 100%;
}

.delete-btn {
  background: linear-gradient(135deg, #F56C6C 0%, #f78989 100%);
  border: none;
  margin-right: 8px;
}

.edit-btn {
  background: linear-gradient(135deg, #409EFF 0%, #66b1ff 100%);
  border: none;
}

/* 分页样式 */
.pagination-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 24px;
  padding: 16px 0;
}

.pagination-info {
  font-size: 14px;
  color: #606266;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .elimination-container {
    padding: 16px;
  }
  
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .search-input {
    width: 100%;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .page-description {
    font-size: 14px;
  }
}
</style> 